package cn.wit.summit.chwan.ui.activity;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;

import com.meituan.android.walle.ChannelInfo;
import com.meituan.android.walle.WalleChannelReader;
import com.umeng.commonsdk.UMConfigure;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.Stat.StatFactory;
import cn.wit.summit.chwan.ui.base.MyActivity;
import cn.wit.summit.chwan.utils.LogUtils;
import cn.wit.summit.chwan.utils.MobileCodeUtils;
import cn.wit.summit.chwan.utils.PreferenceUtil;
import cn.wit.summit.chwan.utils.StringUtils;

/**
 * Created by Alan on 2018/5/21.
 */

public class SplashActivity extends MyActivity {
    private String shareGameId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);

        LogUtils.e("SplashActivity>>>>onCreate");
        MobileCodeUtils.taskStartAppTag(mContext);

        hideTitleBar();

        initAnim();
    }

    private void initAnim() {
//        anim = (AnimationDrawable) getResources().getDrawable(R.drawable.splash_drawable);
//        splashIv.setImageDrawable(anim);
//        anim.start();
//        int duration = 500;
//        for(int i=0;i<anim.getNumberOfFrames();i++){
//            duration += anim.getDuration(i);
//        }
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                String umId = UMConfigure.getUMIDString(mContext);
                if (umId!=null && !StringUtils.isEmpty(umId)) {
                    PreferenceUtil.savePreference(mContext, PreferenceUtil.PRE_UMENG_ID, umId);
                }

                LogUtils.e("SplashActivity>>>>finish");
                Intent intent = new Intent(mContext, MainActivity.class);
                intent.putExtra("shareGameId", shareGameId);
                startActivity(intent);
                finish();
                StatFactory.getInstance().startApp(mContext, getShareCodeByWalle(), "1");
            }
        }, 200);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                shareGameId = getShareGameIdByWalle();
                ChannelInfo channelInfo = WalleChannelReader.getChannelInfo(getApplicationContext());
                if (channelInfo != null) {
                    String channel = channelInfo.getChannel();
                    Map<String, String> extraInfo = channelInfo.getExtraInfo();
                }
                // 或者也可以直接根据key获取
                String value = WalleChannelReader.get(mContext, "shareGame");
            }
        }, 0);
        LogUtils.e("SplashActivity>>>>shareGameId：" + shareGameId);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.slide_right_out);
    }
}
